{% extends 'base.html' %}

{% block content %}

    <form action="" method="post">
        {% csrf_token %}
        <div class="add_article">
            <div class="alert-success text-center">编辑文章</div>

            <div class="add_article_region">
                <div class="title form-group">
                    <label for="">标题</label>
                    <div>
                        <input type="text" name="title" value="{{ article.title }}" id="title">
                    </div>
                </div>

                <div class="content form-group">
                    <label for="">内容(Kindeditor编辑器，不支持拖放/粘贴上传图片) </label>
                    <div>
                        <textarea class="content-1" name="content" id="article_content" cols="30"
                                  rows="10">{{ article.content }}</textarea>
                    </div>
                </div>
                <div>
                    <span class="span_tag">
                        <label for="">标签</label>
                        <input type="text" name="tag" id="tag">
                        <span class="tag_error"></span>
                    </span>

                    <span>
                        <label for="">分类</label>
                        <input type="text" name="category" id="category">
                        <span class="catgory_error"></span>
                    </span>


                    <br>
                    <input type="button" class="btn btn-default" id="submit" article_id="{{ article.nid }}" value="提交">
                </div>

            </div>


        </div>
    </form>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <script charset="utf-8" src="/static/kindeditor/kindeditor-all.js"></script>

    <script>
        //KindEditor文本编辑器
        KindEditor.ready(function (K) {
            window.editor = K.create('#article_content', {
                width: "800",
                height: "600",
                resizeType: 2,
                uploadJson: "/upload",
                extraFileUploadParams: {
                    csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val()
                },
                filePostName: "upload_img",
                allowFileManager: true


            });
        });
        //修改文章
        $(document).ready(
            $("#submit").click(function () {
                editor.sync();
                $.ajax({
                    url: "/update/", //请求url
                    type: "post",//post
                    data: {
                        "title": $("#title").val(),
                        "content": document.getElementById('article_content').value,
                        "article_id": $(this).attr("article_id"),
                        "csrfmiddlewaretoken": $("[name='csrfmiddlewaretoken']").val(),
                    },
                    success: function () { //回调函数
                        location.href = "/{{ user }}"

                    }
                })
            })
        )

    </script>
{% endblock %}